﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using PLAYXPERT.Controls;

namespace SkypePXP
{
    public class SkypeTabControl : Panel
    {
        public List<SkypePanelPage> PanelPages = new List<SkypePanelPage>();
        public ImageList ImageList { get; set; }
        public int SelectedIndex { get; set; }

        bool Initialized = false;

        const int headerHeight = 30;
        const int headerWidth = 100;

        public SkypeTabControl()
        {
            this.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            SelectedIndex = -1;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
             //Paint
            if (!Initialized)
            {
                //Tab Headers
                int Total = 0;
                for (int i = 0; i < PanelPages.Count; i++)
                {
                    //Image
                    PictureBox pic = new PictureBox();
                    pic.BackColor = Color.Transparent;
                    pic.Image = ImageList.Images[PanelPages[i].ImageIndex];
                    pic.Size = new System.Drawing.Size(20, 20);
                    pic.MouseDown += new MouseEventHandler(pic_MouseDown);

                    //Text
                    Label txt = new Label();
                    txt.ForeColor = Color.LightGray;
                    txt.Text = PanelPages[i].Text;
                    txt.Width = 1;
                    txt.AutoSize = true;
                    txt.MouseDown += new MouseEventHandler(pic_MouseDown);

                    //Tab Header
                    Panel Header = new Panel();
                    Header.Tag = PanelPages[i].Text;
                    Header.Controls.AddRange(new Control[] { pic, txt });
                    Header.Size = new System.Drawing.Size(txt.Width + pic.Width + 10, headerHeight);
                    Header.Controls[0].Location = new System.Drawing.Point(5, 5);
                    Header.Controls[1].Location = new System.Drawing.Point(25, 7);
                    Header.MouseDown += new MouseEventHandler(Header_MouseDown);

                    //Position
                    this.Controls.Add(Header);
                    this.Controls[i].Location = new System.Drawing.Point(Total, 0);
                    Total += Header.Size.Width;

                }
                Initialized = true;

                SelectedIndex = PanelPages.Count > 0 ? 0 : -1;
                
                //Tab Panel
                Panel p = new Panel();
                this.Controls.Add(p);
                p.Location = new Point(0, headerHeight);
                p.Size = new Size(p.Parent.Width - 5, p.Parent.Height - headerHeight - 5);
                p.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                p.BackColor = Color.LightSlateGray;
                p.VerticalScroll.Enabled = false;
                p.VerticalScroll.Visible = false;
                p.HorizontalScroll.Enabled = false;
                p.HorizontalScroll.Visible = false;
                p.AutoScroll = false;
                if (SelectedIndex > -1)
                {
                    p.Controls.Add(PanelPages[SelectedIndex]);
                    p.Controls[0].Dock = DockStyle.Fill;
                    SelectGroup((Panel)this.Controls[SelectedIndex]);
                }                   
            }
            base.OnPaint(e);
        }

        void Header_MouseDown(object sender, MouseEventArgs e)
        {
            SkypeTabControl Parent = (SkypeTabControl)((Control)sender).Parent;
            Parent.SelectGroup((Panel)sender);
        }

        void pic_MouseDown(object sender, MouseEventArgs e)
        {
            SkypeTabControl p = (SkypeTabControl)((Control)sender).Parent.Parent;
            p.SelectGroup((Panel)((Control)sender).Parent);
        }

        public void SelectGroup(Panel p)
        {
            //Recolor and Find the Index
            for (int i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i].Location == p.Location)
                {
                    SelectedIndex = i;
                    this.Controls[i].BackColor = Color.LightSlateGray;
                    this.Controls[this.Controls.Count - 1].Controls.Clear();
                    this.Controls[this.Controls.Count - 1].Controls.Add(PanelPages[i]);
                }
                else
                    this.Controls[i].BackColor = Color.Transparent;
            }
        }

    }
}
